﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Collections;
using System.Xml.Linq;
using System.Windows.Forms;

namespace Reticle.Core
{
    public static class PluginFunctions
    {
        public static List<T> GetPlugins<T>(string botKey, string folder)
        {
            XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\files\config.xml");
            List<T> tList = new List<T>();

            foreach (XElement xe in doc.Descendants("Profile"))
            {
                if (xe.Element("id").Value == botKey)
                {
                    foreach (XElement xe2 in xe.Descendants("plugin"))
                    {
                        try
                        {
                            Assembly assembly = Assembly.LoadFile(Environment.CurrentDirectory + @"\files\plugins\" + xe2.Value);

                            foreach (Type type in assembly.GetTypes())
                            {
                                if (!type.IsClass || type.IsNotPublic) continue;
                                Type[] interfaces = type.GetInterfaces();

                                if (((IList)interfaces).Contains(typeof(T)))
                                {
                                    object obj = Activator.CreateInstance(type);
                                    T t = (T)obj;
                                    tList.Add(t);
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                    return tList;
                }
            }

            return null;
        }

        public static List<T> GetSinglePlugin<T>(string botKey, string plugin)
        {
            List<T> tList = new List<T>();

            try
            {
                Assembly assembly = Assembly.LoadFile(plugin);

                foreach (Type type in assembly.GetTypes())
                {
                    if (!type.IsClass || type.IsNotPublic) continue;
                    Type[] interfaces = type.GetInterfaces();

                    if (((IList)interfaces).Contains(typeof(T)))
                    {
                        object obj = Activator.CreateInstance(type);
                        T t = (T)obj;
                        tList.Add(t);
                    }
                }
            }
            catch
            {
            }

            return tList;
        }

        public static ToolStripMenuItem NewToolMenu(string name, string text, EventHandler e)
        {
            ToolStripMenuItem t = new ToolStripMenuItem();
            t.Name = name;
            t.Text = text;
            t.Click += e;
            //t.CheckOnClick = true;
            return t;
        }

        public static ToolStripMenuItem NewToolMenu(string name, string text, EventHandler e, bool ischecked)
        {
            ToolStripMenuItem t = new ToolStripMenuItem();
            t.Name = name;
            t.Text = text;
            t.Click += e;
            t.Checked = ischecked;
            t.CheckOnClick = true;
            return t;
        }

        public static List<string> GetAllPlugins()
        {
            string[] files = Directory.GetFiles(Application.StartupPath + @"\files\plugins", "*.dll");
            List<string> plugin = new List<string>();

            foreach (string file in files)
            {
                    plugin.Add(Path.GetFileName(file));
            }

            return plugin;
        }

        public static List<T> GetAllPluginsBot<T>()
        {
            string[] files = Directory.GetFiles(Application.StartupPath + @"\files\plugins", "*.dll");
                List<T> tList = new List<T>();

                Debug.Assert(typeof(T).IsInterface);

                foreach (string file in files)
                {
                    try
                    {
                        Assembly assembly = Assembly.LoadFile(file);

                        foreach (Type type in assembly.GetTypes())
                        {
                            if (!type.IsClass || type.IsNotPublic) continue;
                            Type[] interfaces = type.GetInterfaces();

                            if (((IList)interfaces).Contains(typeof(T)))
                            {
                                object obj = Activator.CreateInstance(type);
                                T t = (T)obj;
                                tList.Add(t);
                            }
                        }
                    }
                    catch
                    {
                    
                    }
                }
                return tList;
            }

        public static List<string> GetMyPlugins(string botKey)
        {
            XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\files\config.xml");
            List<string> tList = new List<string>();

            foreach (XElement xe in doc.Descendants("Profile"))
            {
                if (xe.Element("id").Value == botKey)
                {
                    foreach (XElement xe2 in xe.Descendants("plugin"))
                    {
                        tList.Add(xe2.Value);
                    }
                    return tList;
                }
            }

            return null;
        }
    }
}